﻿using MvCamCtrl.NET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HikDemo
{
    public partial class Form1 : Form
    {
        Hik hik = new Hik();
        public Form1()
        {
            InitializeComponent();
            try
            {
                hik.DeviceListAcq();
            }
            catch (ArgumentException ex)
            {

            }
        }




        private void bnEnum_Click(object sender, EventArgs e)
        {
            List<string> devices = new List<string>();
            cbDeviceList.Items.Clear();
            try
            {
                devices = hik.DeviceListAcq();
                cbDeviceList.Items.AddRange(devices.ToArray());
                cbDeviceList.SelectedIndex = 0;
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    

        
        


        private void bnOpen_Click(object sender, EventArgs e)
        {
            int index = cbDeviceList.SelectedIndex;
            if (index < 0) return;
            try
            {
                hik.openCamera(index);
            }
            catch(ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            bnGetParam_Click(null, null);// ch:获取参数 | en:Get parameters
            bnStartGrab.Enabled = true;
            bnClose.Enabled = true;
            bnStopGrab.Enabled = false;
            bnSavePic.Enabled = false;
            bnOpen.Enabled = false;

        }

        

        private void bnGetParam_Click(object sender, EventArgs e)
        {
            tbExposure.Text = hik.getParam().Item1;
            tbGain.Text = hik.getParam().Item2;
            tbFrameRate.Text = hik.getParam().Item3;
        }


        private void bnStartGrab_Click(object sender, EventArgs e)
        {
            hik.startGrab(pictureBox1.Handle);
            
            // ch:控件操作 | en:Control Operation
            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = true;
            bnSavePic.Enabled = true;

            
        }





        private void bnStopGrab_Click(object sender, EventArgs e)
        {
            try { 
                hik.stopGrab();
            }
            catch(ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }

            // ch:控件操作 | en:Control Operation
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;
            bnSavePic.Enabled = false;

        }

        private void bnClose_Click(object sender, EventArgs e)
        {
            try
            {
                hik.closeCamera();
            }
            catch(ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }

            // ch:控件操作 | en:Control Operation
            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = false;
            bnSavePic.Enabled = false;
            bnClose.Enabled = false;
            bnStopGrab.Enabled = false;
        
            bnOpen.Enabled = true;
            pictureBox1.Refresh();


        }

        private void bnSavePic_Click(object sender, EventArgs e)
        {
            try
            {
                hik.saveBMP();
                MessageBox.Show("保存成功！");
            }
            catch(ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bnSetParam_Click(object sender, EventArgs e)
        {
            try
            {
                hik.setParam(new Tuple<string, string, string>(tbExposure.Text, tbGain.Text, tbFrameRate.Text));
            }
            catch(ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }

}
